package com.zhuss.vent.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.vent.vent.CreateVentBo;
import com.zhuss.vent.db.dao.ext.VentDao;
import com.zhuss.vent.db.entity.Vent;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.util.JsonUtil;

/**
 * 创建匿名吐槽 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("ventCreate")
public class VentCreate extends AbstractProcessor<Object, CreateVentBo> implements Processor<Object, CreateVentBo> {

    @Autowired
    private VentDao ventDao;
    
    @Autowired
	@Qualifier("ventRead")
	private VentRead ventRead;
    
    
    /**
     * 校验
     */
    @Override
    public void check(CreateVentBo bo) throws WpsException {
    	String title = bo.getTitle();
    	if (title == null || "".equals(title.trim())) {
    		throw new WpsException(ExceptionCode.匿名吐槽标题为空);
    	}
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(CreateVentBo param) {
    	Vent record = new Vent();
    	record.setTitle(param.getTitle());
    	record.setContent(param.getContent());
    	record.setPic(param.getPic());
    	record.setSort(param.getSort());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	ventDao.create(record);
    	param.setTargetId(record.getVentId());
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CreateVentBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.匿名吐槽.key());
    	record.setAction(LogActionEnum.匿名吐槽创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getTitle());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
